// pages/center/center.js
const app = getApp();
var util =  require('../../utils/util.js');
var loadMoreView,p = 1,num = app.globalData.pageNum,type = 'activity'
Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    util.setNavBarColor("#ffffff",app.globalData.themeColor);
    //社区id
    if(app.globalData.communityId){
      that.setData({
        communityId: app.globalData.communityId
      });
      that.getCenterBanner()
      that.getPropertyNotice()
      that.getActivities()
    }else{
      app.communityIdCallback = communityId => {
        if (communityId != '') {
          that.setData({
            communityId: communityId
          });
          that.getCenterBanner()
          that.getPropertyNotice()
          that.getActivities()
        }
      }
    }
  },

  //获取banner
  getCenterBanner:function(){
    util.getAjax(app.globalData.apiUrl +'/get_community_member_center_banner',this.getBanner,{community_id: this.data.communityId})
  },

  //获取banner
  getBanner(res){
    var that = this
    if(res.data.length != 0){
      var bannerImgList = []
      for(var i in res.data){
        bannerImgList.push(res.data[i])
      }
      that.setData({
        isBanner: true,
        bannerImgList: bannerImgList,
        currentSwiper: 0
      })
    }
  },

  swiperChange:function(e){
    this.setData({
      currentSwiper: e.detail.current
    })
  },

  //获取物业公告
  getPropertyNotice:function(){
    util.getAjax(app.globalData.apiUrl +'/get_community_member_center_property_notice',this.getNotice,{type:1,community_id: this.data.communityId})
  },

  getNotice(res){
    // console.log(res)
    if(res.data){
      res.data.date = res.data.date.substring(0,10);
      this.setData({
        isProperty: true,
        propertyList: res.data
      })
    }
  },

  //获取社员中心活动
  getActivities:function(){
    util.getAjax(app.globalData.apiUrl +'/get_community_member_center_hd_news',this.getActivity,{community_id: this.data.communityId,p: p,num: num},this.getActivityErr)
  },

  getActivity(res){
    // console.log(res)
    var that = this 
    if(res.data.list.length != 0){
      var list = []
      if(p == 1) {
        list = res.data.list
      } else {
        list = that.data.activityList.concat(res.data.list)
      }
      that.setData({
        isActivity: true,
        activityList: list
      })
      that.setAllConten(that.data.activityList)
    }else{
      that.setData({
        isActivity: false,
        activityList: []
      })
    }
    loadMoreView = this.selectComponent("#loadMoreView")
    loadMoreView.loadMoreComplete(res.data,type)
  },

  //设置全文
  setAllConten:function(list){
    for(var i in list){
      // console.log(list[i].content.length)
      if(list[i].content.length > 120){
        list[i].isAll = true
        list[i].isClamp = true
        list[i].allTxt = '全文'
        list[i].allSrc = '/resources/mine/down.png'
      }else{
        list[i].isAll = false
        list[i].isClamp = false
      }
    }
    this.setData({
      activityList: list
    })
  },

  //全文折叠
  clampBtn:function(e){
    var that = this
    var id = e.currentTarget.dataset.id
    var list = that.data.activityList
    for(var i in list){
      if(list[i].id == id && list[i].isAll){
        if(list[i].isClamp){
          list[i].isClamp = false
          list[i].allTxt = '收起'
          list[i].allSrc = '/resources/mine/up.png'
        }else{
          list[i].isClamp = true
          list[i].allTxt = '全文'
          list[i].allSrc = '/resources/mine/down.png'
        }
      }
    }
    that.setData({
      activityList: list
    })
  },
  
  //获取商品数据异常
  getActivityErr(err){
    if(p>1){
      loadMoreView.loadMoreFail()
    }else{
      util.showModal('提示','获取活动新闻失败，请向上滑动屏幕')
      this.setData({
        isFail: true
      })
    }
  },

  loadMoreListener: function(e) {
    p += 1
    this.getActivities()
  },

  clickLoadMore: function(e) {
    this.getActivities()
  },

  //活动
  activityBtn:function(e){ 
    var type = e.currentTarget.dataset.type
    switch (type) {
      case 'publicWelfare':
        wx.navigateTo({
          url: '/subPages/center/communityPublicWelfare/communityPublicWelfare',
        })
        break;
      case 'activity':
        wx.navigateTo({
          url: '/subPages/center/communityActivities/communityActivities',
        })
        break;
    }
  },

  //物业公告
  propertyBtn:function(){
    if(this.data.isProperty){
      wx.navigateTo({
        url: '/subPages/center/property/property',
      })
    }else{
      util.showModal('提示','暂无物业公告')
      return
    }
  },

  //物业详情
  property:function(){
    wx.navigateTo({
      url: '/subPages/center/propertyDetails/propertyDetails?id=' + this.data.propertyList.id,
    })
  },

  //点击图片
  imgBtn:function(e){
    // console.log(e)
    var list = e.currentTarget.dataset.list;
    var index = e.currentTarget.dataset.index;
    let arr = []
    for(var i in list){
      arr.push(list[i].url)
    }
    wx.previewImage({
      urls: arr,
      current: arr[index]
    })
  },

  //立即参与
  activitybtn:function(){
    wx.navigateTo({
      url: '/subPages/center/communityActivities/communityActivities',
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    if(that.data.communityId){
      that.getCenterBanner()
      that.getPropertyNotice()
      that.getActivities()
    }
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isFail){
      this.getActivities()
      return
    }
    loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})